﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __18850E12_BDCF_495a_82DA_6C1934107DA5__
#define __18850E12_BDCF_495a_82DA_6C1934107DA5__

#include "../macro/head.h"
#include "../release/head.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos
{
    // 
    // tcp interface
    // 
    class i_socket : public i_release
    {
    public:
        enum enumShutdown
        {
            XOS_SD_RECEIVE,
            XOS_SD_SEND,
            XOS_SD_BOTH
        };

    public:
        virtual ~i_socket(){}

    public:
        // 
        // udp端口初始化
        // 
        virtual int udp_init( const char * lpszLocalIp, unsigned short usLocalPort ) = 0;

        // 
        // 返回值，为发送的字节数, -1表示失败
        // 
        virtual int udp_send( const char * lpBuf, int nLen, const char * lpszToIp, unsigned short usToPort ) = 0;

        // 
        // 返回值，为接收到的字节数, -1表示失败
        // 
        virtual int udp_recv( char * lpBuf, int nLen, char * lpszFromIp, unsigned short * lpusFromPort ) = 0;

        // 
        // tcp服务端端口初始化
        // 
        virtual int tcp_init( const char * lpszLocalIp, unsigned short usLocalPort, bool bBind ) = 0;

        // 
        // accept
        // 
        virtual int tcp_accept( i_socket ** ppv ) = 0;

        // 
        // connect
        // 
        virtual int tcp_connect( const char * lpszServerIp, unsigned short usServerPort ) = 0;

        // 
        // tcp侦听
        // 
        virtual int tcp_listen( int nNum ) = 0;

        // 
        // 返回值，为发送的字节数, -1表示失败
        // 
        virtual int tcp_send( const char * lpBuf, int nLen ) = 0;

        // 
        // 返回值，为接收到的字节数, -1表示失败
        // 
        virtual int tcp_recv( char * lpBuf, int nLen ) = 0;

        // 
        // 设置异步/阻塞模式
        // 
        virtual int set_async( bool bAsync ) = 0;

        // 
        // 关闭端口
        // 
        virtual int close( int onoff, int linger ) = 0;

        // 
        // 停止后续发送
        // 
        virtual int shut_down( int how ) = 0;

        // 
        // 根据机器名得到IP
        // 
        virtual int get_ip_by_name( const char * lpszName, unsigned long * lpdwIp, char * lpszIp ) = 0;
        virtual int get_ip_by_name( const char * lpszName, char * lpszIP ) = 0;
        virtual int get_local_addr( char * lpszName, char * lpszIP ) = 0;

        // 
        // 根据socket句柄得到IP和端口
        // 
        virtual int get_socket_addr( char * lpszIp, unsigned short * lpusPort ) = 0;
        virtual int get_peer_addr( char * lpszIp, unsigned short * lpusPort ) = 0;

        // 
        // 返回设置进去的IP和端口
        // 
        virtual int get_ip_port( char * lpszIp, unsigned short * lpusPort ) = 0;
        virtual void set_addr( void * lpAddr ) = 0;

        // 
        // 取得socket句柄
        // 
        virtual void set_socket( xos::xos_u64 ulSocket ) = 0;
        virtual xos_u64 get_socket() = 0;

        // 
        // get user data
        // 
        virtual void * get_user_data( void ** ppvUserData ) = 0;

        // 
        // set user data
        // 
        virtual int set_user_data( void * pUserData ) = 0;

        // 
        // release
        // 
        virtual int init() = 0;
        virtual int term() = 0;
    };
}

#endif // __18850E12_BDCF_495a_82DA_6C1934107DA5__
